// 全局状态对象
const stats = {
    correct: 0,
    incorrect: 0
};

// 显示状态信息
function showStatus(message, isSuccess) {
    const status = document.getElementById('status');
    if (status) {
        status.textContent = message;
        status.className = 'status ' + (isSuccess ? 'success' : 'error');
        status.style.display = 'block';
        setTimeout(() => {
            status.style.display = 'none';
        }, 3000);
    }
}

// 更新统计信息
function updateStats() {
    const correctElement = document.getElementById('correctCount');
    const incorrectElement = document.getElementById('incorrectCount');
    const accuracyElement = document.getElementById('accuracy');
    
    if (correctElement && incorrectElement && accuracyElement) {
        correctElement.textContent = stats.correct;
        incorrectElement.textContent = stats.incorrect;
        
        const total = stats.correct + stats.incorrect;
        const accuracy = total > 0 ? ((stats.correct / total) * 100).toFixed(1) : '0.0';
        accuracyElement.textContent = accuracy + '%';
    }
}

// 禁用/启用按钮
function disableButtons(disabled) {
    const upButton = document.getElementById('upButton');
    const downButton = document.getElementById('downButton');
    if (upButton && downButton) {
        upButton.disabled = disabled;
        downButton.disabled = disabled;
    }
}

// 格式化时间
function formatTime(seconds) {
    return seconds.toString().padStart(2, '0');
}

// 改进计时器处理
let timerInterval;
function startTimer(duration = 5) {
    clearInterval(timerInterval); // 确保清除之前的计时器
    
    const timerDisplay = document.getElementById('timer');
    if (!timerDisplay) return;

    let timeLeft = duration;
    timerDisplay.textContent = formatTime(timeLeft);
    timerDisplay.style.display = 'block';

    timerInterval = setInterval(() => {
        timeLeft--;
        if (timeLeft < 0) {
            clearInterval(timerInterval);
            handleTimeout();
            return;
        }
        timerDisplay.textContent = formatTime(timeLeft);
    }, 1000);
}

// 处理超时
function handleTimeout() {
    clearInterval(timerInterval);
    stats.incorrect++;
    updateStats();
    showStatus('超时！', false);
    disableButtons(true);
    setTimeout(loadNewImage, 2000);
}

// 加载新图片
function loadNewImage() {
    fetch('/api/random-image')
        .then(response => {
            if (!response.ok) {
                throw new Error('Failed to load image');
            }
            return response.json();
        })
        .then(data => {
            const imageElement = document.getElementById('currentImage');
            if (imageElement) {
                imageElement.src = '/api/images/' + data.path;
                currentImageInfo = data;
                
                const resultElement = document.getElementById('result');
                if (resultElement) {
                    resultElement.textContent = data.result;
                }
            }
        })
        .catch(error => {
            console.error('Error loading new image:', error);
            showStatus('加载图片失败！', false);
            disableButtons(true);
        });
}

// 页面离开时清理计时器
window.addEventListener('beforeunload', () => {
    clearInterval(timerInterval);
}); 